package main

import (
	"fmt"
	"time"
)

func main() {
	//带有缓冲的channel 只能获取 指定长度的缓冲
	//那么原理是什么呢?
	c := make(chan int, 3)
	fmt.Println("len(c) = ", len(c), ", cap(c)", cap(c))
	go func() {
		defer fmt.Println("\"子go程结束\"")
		for i := 0; i < 4; i++ {
			//往缓冲中填写数值 当满了以后 会阻塞等待空余空间
			c <- i
			fmt.Println("子go程正在运行, 发送的元素=", i, " len(c)=", len(c), ", cap(c)=", cap(c))
		}
	}()
	time.Sleep(2 * time.Second)
	for i := 0; i < 4; i++ {
		//获取时 会阻塞等待数值
		num := <-c
		fmt.Println("num = ", num)
	}
	fmt.Println("main 结束")
}
